<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>考勤统计分析</title>
    <!--layui引用-->
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--表格-->
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/javascript">
    layui.use('table', function () {
        var table = layui.table
            , $ = layui.jquery;

        var count1 = []
            , count2 = []
            , count3 = []
            , count4 = [];
        $.ajax({
            type: "get",
            url: "/atb/count.do",
            async: false,
            success: function (ret) {
                ret = eval("(" + ret + ")");
                console.log(ret);
                for (var i = 0; i < 5; i++) {
                    count1[i] = ret['count'][1][i];
                    count2[i] = ret['count'][2][i];
                    count3[i] = ret['count'][3][i];
                    count4[i] = ret['count'][4][i];
                }
            }
        });

        //展示已知数据
        table.render({
            elem: '#test'
            , cellMinWidth: 80
            , cols: [[ //标题栏
                {field: 'no', title: '学号', sort: true}
                , {field: 'name', title: '姓名'}
                , {field: 'normal', title: '正常', sort: true}
                , {field: 'late', title: '迟到', sort: true}
                , {field: 'leaveEarly', title: '早退', sort: true}
                , {field: 'leave', title: '请假', sort: true}
                , {field: 'truancy', title: '旷课', sort: true}
            ]]
            , data: [{
                "no": 1
                , "name": "安天宝"
                , "normal": count1[0]
                , "late": count1[1]
                , "leaveEarly": count1[2]
                , "leave": count1[3]
                , "truancy": count1[4]
            }, {
                "no": 2
                , "name": "范天明"
                , "normal": count2[0]
                , "late": count2[1]
                , "leaveEarly": count2[2]
                , "leave": count2[3]
                , "truancy": count2[4]
            }, {
                "no": 3
                , "name": "方宇"
                , "normal": count3[0]
                , "late": count3[1]
                , "leaveEarly": count3[2]
                , "leave": count3[3]
                , "truancy": count3[4]
            }, {
                "no": 4
                , "name": "张敏"
                , "normal": count4[0]
                , "late": count4[1]
                , "leaveEarly": count4[2]
                , "leave": count4[3]
                , "truancy": count4[4]
            }]
            , even: true
        });

        //柱状图
        var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        var app = {};
        // option = null;
        var posList = [
            'left', 'right', 'top', 'bottom',
            'inside',
            'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
            'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
        ];

        app.configParameters = {
            rotate: {
                min: -90,
                max: 90
            },
            align: {
                options: {
                    left: 'left',
                    center: 'center',
                    right: 'right'
                }
            },
            verticalAlign: {
                options: {
                    top: 'top',
                    middle: 'middle',
                    bottom: 'bottom'
                }
            },
            position: {
                options: echarts.util.reduce(posList, function (map, pos) {
                    map[pos] = pos;
                    return map;
                }, {})
            },
            distance: {
                min: 0,
                max: 100
            }
        };

        app.config = {
            rotate: 90,
            align: 'left',
            verticalAlign: 'middle',
            position: 'insideBottom',
            distance: 15,
            onChange: function () {
                var labelOption = {
                    normal: {
                        rotate: app.config.rotate,
                        align: app.config.align,
                        verticalAlign: app.config.verticalAlign,
                        position: app.config.position,
                        distance: app.config.distance
                    }
                };
                myChart.setOption({
                    series: [{
                        label: labelOption
                    }, {
                        label: labelOption
                    }, {
                        label: labelOption
                    }, {
                        label: labelOption
                    }]
                });
            }
        };

        var labelOption = {
            normal: {
                show: true,
                position: app.config.position,
                distance: app.config.distance,
                align: app.config.align,
                verticalAlign: app.config.verticalAlign,
                rotate: app.config.rotate,
                formatter: '{c}  {name|{a}}',
                fontSize: 16,
                rich: {
                    name: {
                        textBorderColor: '#fff'
                    }
                }
            }
        };

        var option = {
            color: ['#003366', '#006699', '#4cabce', '#e5323e', '#39809B'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['正常', '迟到', '早退', '请假', '旷课']
            },
            toolbox: {
                show: true,
                orient: 'vertical',
                left: 'right',
                top: 'center',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            calculable: true,
            xAxis: [
                {
                    type: 'category',
                    axisTick: {show: false},
                    data: ['安天宝', '范天明', '方宇', '张敏']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: '正常',
                    type: 'bar',
                    barGap: 0,
                    label: labelOption,
                    data: [count1[0], count2[0], count3[0], count4[0]]
                },
                {
                    name: '迟到',
                    type: 'bar',
                    label: labelOption,
                    data: [count1[1], count2[1], count3[1], count4[1]]
                },
                {
                    name: '早退',
                    type: 'bar',
                    label: labelOption,
                    data: [count1[2], count2[2], count3[2], count4[2]]
                },
                {
                    name: '请假',
                    type: 'bar',
                    label: labelOption,
                    data: [count1[3], count2[3], count3[3], count4[3]]
                },
                {
                    name: '旷课',
                    type: 'bar',
                    label: labelOption,
                    data: [count1[4], count2[4], count3[4], count4[4]]
                }
            ]
        };

        if (option && typeof option === "object") {
            myChart.setOption(option, true);
        }
    });
</script>
<!--柱状图显示-->
<div id="container" style="height: 600%"></div>
<!--引用echarts-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts-en.common.js"></script>
</body>
</html>
